home *** CD-ROM | disk | FTP | other *** search
- #ifndef __CRECT__
- #define __CRECT__
-
- #include "CPoint.h"
-
- #include <QuickDraw.h>
-
- class CRect
- {
- public:
- // constructors
- CRect();
- CRect(const CRect &);
- CRect(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b);
- CRect(CPoint leftTop, CPoint rightBottom);
- CRect(const Rect& r);
-
- // destructor
- inline ~CRect() {};
-
- // assignment
- inline CRect& operator=(const CRect &from);
-
- // conversion
- inline operator Rect*();
- inline operator const Rect*() const;
- inline operator Rect() const;
-
- // accessors
- inline GraphicalUnit Width() const;
- inline GraphicalUnit Height() const;
- inline GraphicalUnit Left() const;
- inline GraphicalUnit Top() const;
- inline GraphicalUnit Right() const;
- inline GraphicalUnit Bottom() const;
-
- inline CPoint LeftTop() const;
- inline CPoint RightBottom() const;
- inline CPoint LeftBottom() const;
- inline CPoint RightTop() const;
-
- // mutators
- inline void Set(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b);
- inline void SetWidth(GraphicalUnit width);
- inline void SetHeight(GraphicalUnit height);
- inline void SetTop(GraphicalUnit top);
- inline void SetBottom(GraphicalUnit bottom);
- inline void SetLeft(GraphicalUnit left);
- inline void SetRight(GraphicalUnit right);
-
- void SetLeftTop(const CPoint);
- void SetRightBottom(const CPoint);
- void SetLeftBottom(const CPoint);
- void SetRightTop(const CPoint);
-
- // transformation
-
- void InsetBy(CPoint);
- inline void InsetBy(GraphicalUnit inset);
- void InsetBy(GraphicalUnit dx, GraphicalUnit dy);
- void OffsetBy(CPoint);
- void OffsetBy(GraphicalUnit dx, GraphicalUnit dy);
- void OffsetTo(CPoint);
- void OffsetTo(GraphicalUnit x, GraphicalUnit y);
-
- // comparison
- Boolean operator==(CRect) const;
- Boolean operator!=(CRect) const;
-
- // intersection and union
- CRect operator&(CRect) const;
- CRect operator|(CRect) const;
-
- // utilities
- Boolean Intersects(CRect r) const;
- inline Boolean IsValid() const;
- inline Boolean IsEmpty() const;
- Boolean Contains(CPoint) const;
- Boolean Contains(CRect) const;
- void Center(CRect outsideRect, Boolean scaleIfNeeded = true);
-
- private:
- Rect fRect;
- };
-
- //------------------------------------------------------------------------------
-
- inline CRect::CRect()
- {
- // make initial rect invalid
- fRect.top = fRect.left = 0;
- fRect.bottom = fRect.right = -1;
- }
-
- inline CRect::CRect(const CRect &r)
- {
- fRect.left = r.Left();
- fRect.top = r.Top();
- fRect.right = r.Right();
- fRect.bottom = r.Bottom();
- }
-
- inline CRect::CRect(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b)
- {
- fRect.left = l;
- fRect.top = t;
- fRect.right = r;
- fRect.bottom = b;
- }
-
- inline CRect::CRect(CPoint leftTop, CPoint rightBottom)
- {
- fRect.left = leftTop.X();
- fRect.top = leftTop.Y();
- fRect.right = rightBottom.X();
- fRect.bottom = rightBottom.Y();
- }
-
- inline CRect::CRect(const Rect& r)
- {
- fRect.left = r.left;
- fRect.top = r.top;
- fRect.right = r.right;
- fRect.bottom = r.bottom;
- }
-
- inline CRect &CRect::operator=(const CRect& from)
- {
- // don't need to worry about "this==from"
- fRect.left = from.fRect.left;
- fRect.top = from.fRect.top;
- fRect.right = from.fRect.right;
- fRect.bottom = from.fRect.bottom;
- return *this;
- }
-
- inline void CRect::Set(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b)
- {
- fRect.left = l;
- fRect.top = t;
- fRect.right = r;
- fRect.bottom = b;
- }
-
- inline CRect::operator Rect*()
- {
- return &fRect;
- }
-
- inline CRect::operator const Rect*() const
- {
- return (Rect* const)&fRect;
- }
-
- inline CRect::operator Rect() const
- {
- return fRect;
- }
-
- inline GraphicalUnit CRect::Width() const
- {
- return (Right() - Left());
- }
-
- inline GraphicalUnit CRect::Height() const
- {
- return (Bottom() - Top());
- }
-
- inline GraphicalUnit CRect::Left() const
- {
- return fRect.left;
- }
-
- inline GraphicalUnit CRect::Top() const
- {
- return fRect.top;
- }
-
-
- inline GraphicalUnit CRect::Right() const
- {
- return fRect.right;
- }
-
-
- inline GraphicalUnit CRect::Bottom() const
- {
- return fRect.bottom;
- }
-
-
- inline CPoint CRect::LeftTop() const
- {
- return(CPoint(Left(), Top()));
- }
-
- inline CPoint CRect::RightBottom() const
- {
- return(CPoint(Right(), Bottom()));
- }
-
- inline CPoint CRect::LeftBottom() const
- {
- return(CPoint(Left(), Bottom()));
- }
-
- inline CPoint CRect::RightTop() const
- {
- return(CPoint(Right(), Top()));
- }
-
- inline void CRect::SetWidth(GraphicalUnit w)
- {
- fRect.right = Left() + w;
- }
-
-
- inline void CRect::SetHeight(GraphicalUnit h)
- {
- fRect.bottom = Top() + h;
- }
-
- inline void CRect::SetTop(GraphicalUnit top)
- {
- fRect.top = top;
- }
-
- inline void CRect::SetBottom(GraphicalUnit bottom)
- {
- fRect.bottom = bottom;
- }
-
- inline void CRect::SetLeft(GraphicalUnit left)
- {
- fRect.left = left;
- }
-
- inline void CRect::SetRight(GraphicalUnit right)
- {
- fRect.right = right;
- }
-
- inline void CRect::InsetBy(GraphicalUnit inset)
- {
- InsetBy(inset, inset);
- }
-
-
- inline Boolean CRect::IsValid() const
- {
- if (Left() <= Right() && Top() <= Bottom())
- return true;
- else
- return false;
- }
-
- inline Boolean CRect::IsEmpty() const
- {
- if (Left() == Right() && Top() == Bottom())
- return true;
- else
- return false;
- }
-
- #endif
-